package com.summer.safe.controller.system;

import com.summer.safe.base.BaseController;
import com.summer.safe.base.Page;
import com.summer.safe.config.annotation.ResultWrapper;
import com.summer.safe.config.result.ResultCode;
import com.summer.safe.system.entity.Dict;
import com.summer.safe.system.entity.DictType;
import com.summer.safe.system.service.DictService;
import com.summer.safe.system.service.DictTypeService;
import com.summer.safe.utils.ToolUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 字典类型管理Controller
 *
 */
@Controller
@RequestMapping(value = "/sys/dictType")
public class DictTypeController extends BaseController {

    private String PREFIX = "/system/dict/";
    @Autowired
    private DictTypeService dictTypeService;
    @Autowired
    private DictService dictService;


    @ModelAttribute("dictType")
    public DictType get(@RequestParam(required=false) String id) {
        if (StringUtils.isNotBlank(id)){
            return dictTypeService.get(id);
        }else{
            return new DictType();
        }
    }

    /**
     * 字典列表类型
     * @param model
     * @return
     */
    @RequiresPermissions("sys:dictType:view")
    @GetMapping("")
    public String index(Model model) {
        return  PREFIX+"type/typelist.html";
    }


    /**
     * 字典类型
     * @param dictType
     * @param request
     * @return
     */
    @RequestMapping("/dataList")
    @ResultWrapper
    public Page typeList(DictType dictType, HttpServletRequest request) {

        Page<Dict> page = dictTypeService.findPage(new Page(request), dictType);

        return page;
    }



    /**
     * 字典类型
     * @param dictType
     * @param model
     * @return
     */
    @RequiresPermissions("sys:dictType:edit")
    @GetMapping("/form")
    public String form(DictType dictType, Model model) {
        model.addAttribute("dictType", dictType);
        return PREFIX+"type/typeform.html";
    }



    /**
     *字典类型
     * @param dictType
     */
    @PostMapping("/save")
    @ResultWrapper(ResultCode.SUCCESS_SAVE)
    public void save(DictType dictType) {
        //判断是否为演示模式
        ToolUtil.DemoMode();
        dictTypeService.save(dictType);


    }
    /**
     *字典类型
     * @param dictType
     */
    @RequestMapping("/delete")
    @ResultWrapper(ResultCode.SUCCESS_DELETE)
    public void delete(DictType dictType) {
        //
        dictTypeService.delete(dictType);
        //删除的字典列表
        dictService.deleteType(dictType.getType());
    }


    /**
     * 检测字典类型是否存在
     */
    @PostMapping("/checkType")
    @ResultWrapper(ResultCode.DICT_TYPE_ERROR)
    public void checkType(String type) {
        dictTypeService.getCheckType(type);
    }

}